﻿import pygame
import sys

# 初始化
pygame.init()
SCREEN_WIDTH, SCREEN_HEIGHT = 800, 600
screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
pygame.display.set_caption("魔塔Demo")

# 游戏常量
TILE_SIZE = 32
HERO_SPEED = 4

# 颜色定义
WALL_COLOR = (100, 100, 200)
FLOOR_COLOR = (50, 50, 50)
HERO_COLOR = (255, 200, 0)

# 简易地图 (0=空地, 1=墙, 2=门, 3=怪物)
current_floor = [
    [1, 1, 1, 1, 1],
    [1, 0, 0, 2, 1],
    [1, 3, 0, 0, 1],
    [1, 0, 0, 3, 1],
    [1, 1, 1, 1, 1]
]

# 玩家属性
hero = {
    "x": 1 * TILE_SIZE,
    "y": 1 * TILE_SIZE,
    "atk": 10,
    "def": 5,
    "hp": 100,
    "keys": 0
}

def draw_game():
    """绘制游戏画面"""
    screen.fill((0, 0, 0))
    
    # 绘制地图
    for y, row in enumerate(current_floor):
        for x, tile in enumerate(row):
            rect = pygame.Rect(x*TILE_SIZE, y*TILE_SIZE, TILE_SIZE, TILE_SIZE)
            if tile == 1:
                pygame.draw.rect(screen, WALL_COLOR, rect)
            elif tile == 2:
                pygame.draw.rect(screen, (200, 100, 50), rect)  # 门
            elif tile == 3:
                pygame.draw.rect(screen, (200, 0, 0), rect)     # 怪物
    
    # 绘制玩家
    pygame.draw.rect(screen, HERO_COLOR, 
                    (hero["x"], hero["y"], TILE_SIZE, TILE_SIZE))
    
    pygame.display.flip()

def handle_movement():
    """处理玩家移动"""
    keys = pygame.key.get_pressed()
    new_x, new_y = hero["x"], hero["y"]
    
    if keys[pygame.K_LEFT]: new_x -= HERO_SPEED
    if keys[pygame.K_RIGHT]: new_x += HERO_SPEED
    if keys[pygame.K_UP]: new_y -= HERO_SPEED
    if keys[pygame.K_DOWN]: new_y += HERO_SPEED
    
    # 简易碰撞检测
    tile_x, tile_y = new_x // TILE_SIZE, new_y // TILE_SIZE
    if current_floor[tile_y][tile_x] != 1:  # 不是墙才能移动
        hero["x"], hero["y"] = new_x, new_y



if __name__ == "__main__":
    # 主循环
    clock = pygame.time.Clock()
    running = True

    while running:
        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                running = False
    
        handle_movement()
        draw_game()
        clock.tick(60)

    pygame.quit()